package business.inhospitalqc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;
import java.util.HashMap;

import static common.Login.login;
import static common.TestStep.requestAndRespondBody;


public class TemplateResultList {
    Logger log = Logger.getLogger(TemplateResultList.class);
    public static final String system = "env.properties";
//    CreateTemplateResult createTemplateResult=new CreateTemplateResult();
    Data data = login();

    /**
     * @param i api-sourcedata/dmp/template/result/list的datas
     * @param templateId api-sourcedata/dmp/template/create 返回的datas的templateId
     * @return java.util.HashMap<java.lang.String, java.lang.String>
     * @Description: 数据集 - 列表
     * @author gujinle
     * @date 2021-05-06 18:05
     */
    public HashMap<String,String> GetPatientList(String templateId,int i) throws FileNotFoundException, ConfigurationException, YamlException {
//        String templateId= createTemplateResult.CreateModelResult(j);
        String path=new GetFileMess().getValue("ymlPath",system);
        Yaml yaml= YamlReadUtil.getLocatorMap(path,this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName",data.getUserName());
        json.put("page", yaml.getDatas().get(i).get("page"));
        json.put("size", yaml.getDatas().get(i).get("size"));
        json.put("templateId", templateId);
        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, "get", yaml.getUri());
        String jsonStr = response.asString();

        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath +  yaml.getUri();
        requestAndRespondBody(url,json,jsonStr);  //写进allure报告

        log.info("响应报文：" + jsonStr);
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject dataObject = jsonObject.getJSONObject("data");
        JSONObject pageObject = dataObject.getJSONObject("dataPage");
        JSONArray contenArray = pageObject.getJSONArray("content");
        JSONObject obj = contenArray.getJSONObject(0);
        /*4.0以上版本，返参是英文*/
//        String inpatientNo = obj.getString("inpatientNo");

        /*3.7版本，返参是英文*/
        String inpatientNo = obj.getString("住院流水号");

        HashMap map=new HashMap();
        map.put("templateId",templateId);
        map.put("inpatientNo",inpatientNo);
        return map;
    }

    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {
        TemplateResultList tl=new TemplateResultList();
//        HashMap map=tl.GetPatientList(0,1);
//        System.out.println(map.toString());
    }
}
